﻿using System;
using System.Text;
using UtilZ.Dotnet.Ex.Attributes;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.Communication.Net.Ftp;
using UtilZ.Dotnet.Ex.Constants;
using UtilZ.Dotnet.Ex.Log;
using UtilZ.Dotnet.Ex.ValueDescription;

namespace ConAppCore.Test
{
    [ValueDescriptionGroup("测试常量", "真的是一个测试常量", typeof(TestValueDescriptionManager))]
    internal class TestValueDescriptionManager : ExtendDescriptionAbs
    {
        public static void Test()
        {
            Test0();
            //Test1();
            //Test2();
            //Tesst3();
            Test4();
        }


        private static void Test4()
        {
            var group = new ValueDescriptionGroup<int>(new TestValueDescriptionManager(), typeof(TestValueDescriptionManager));
            group.Add(new ConstantValueItem("值11", VALUE1));
            group.Add(new ConstantValueItem("值22", VALUE2));

            const int VALUE3 = 3;
            string name1 = group.GetNameByValue(VALUE1);
            string name2 = group.GetNameByValue(VALUE2);
            string name3;
            if (group.TryGetNameByValue(VALUE3, out name3))
            {
                Console.WriteLine("ok");
            }
            else
            {
                Console.WriteLine("fail");
            }


            try
            {
                string name32 = group.GetNameByValue(VALUE3);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"name32 fail,{ex.Message}");
            }


            //----------------------------------------------------------------------------------------------------

            try
            {
                string des1 = group.GetDescriptionByValue(VALUE1);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"des1 fail,{ex.Message}");
            }

            string des2 = group.GetDescriptionByValue(VALUE2);
            string des3;
            if (group.TryGetDescriptionByValue(VALUE3, out des3))
            {
                Console.WriteLine("ok");
            }
            else
            {
                Console.WriteLine("des3 fail");
            }

            try
            {
                string des32 = group.GetDescriptionByValue(VALUE3);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"fail,{ex.Message}");
            }
        }

        private static void Tesst3()
        {
            string disName = WindowsErrnoConstant.GetName(WindowsErrnoConstant.E2BIG);
            string des = WindowsErrnoConstant.GetDes(WindowsErrnoConstant.EAGAIN);
            string str = FtpStatusCodes.GetNameByValue(FtpStatusCodes.Code553);
        }


        private static void Test2()
        {
            const int VALUE3 = 3;
            var group = new ValueDescriptionGroup<int>(typeof(TestValueDescriptionManager), "abc");

            StringBuilder sb = new StringBuilder();
            foreach (var item in group)
            {
                sb.AppendLine($"{item.Value}        {item.DisplayName}");
            }
            string str = sb.ToString();

            string name1 = group.GetNameByValue(VALUE1);
            string name2 = group.GetNameByValue(VALUE2);
            string name3;
            if (group.TryGetNameByValue(VALUE3, out name3))
            {
                Console.WriteLine("ok");
            }
            else
            {
                Console.WriteLine("fail");
            }



            try
            {
                string name32 = group.GetNameByValue(VALUE3);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"fail,{ex.Message}");
            }


            //----------------------------------------------------------------------------------------------------

            string des1 = group.GetNameByValue(VALUE1);
            string des2 = group.GetDescriptionByValue(VALUE2);
            string des3;
            if (group.TryGetDescriptionByValue(VALUE3, out des3))
            {
                Console.WriteLine("ok");
            }
            else
            {
                Console.WriteLine("fail");
            }

            try
            {
                string des32 = group.GetDescriptionByValue(VALUE3);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"fail,{ex.Message}");
            }
        }

        private static void Test1()
        {
            const int VALUE3 = 3;
            ValueDescriptionManager.Registe<int, TestValueDescriptionManager>();

            string name1 = ValueDescriptionManager.GetNameByValue<TestValueDescriptionManager>(VALUE1);
            string name2 = ValueDescriptionManager.GetNameByValue<TestValueDescriptionManager>(VALUE2);
            string name3;
            if (ValueDescriptionManager.TryGetNameByValue<TestValueDescriptionManager>(VALUE3, out name3))
            {
                Console.WriteLine("ok");
            }
            else
            {
                Console.WriteLine("fail");
            }


            try
            {
                string name32 = ValueDescriptionManager.GetNameByValue<TestValueDescriptionManager>(VALUE3);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"name32 fail,{ex.Message}");
            }


            //----------------------------------------------------------------------------------------------------

            try
            {
                string des1 = ValueDescriptionManager.GetDescriptionByValue<TestValueDescriptionManager>(VALUE1);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"des1 fail,{ex.Message}");
            }

            string des2 = ValueDescriptionManager.GetDescriptionByValue<TestValueDescriptionManager>(VALUE2);
            string des3;
            if (ValueDescriptionManager.TryGetDescriptionByValue<TestValueDescriptionManager>(VALUE3, out des3))
            {
                Console.WriteLine("ok");
            }
            else
            {
                Console.WriteLine("des3 fail");
            }

            try
            {
                string des32 = ValueDescriptionManager.GetDescriptionByValue<TestValueDescriptionManager>(VALUE3);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"fail,{ex.Message}");
            }
        }

        private static void Test0()
        {
            var dataPackgeType = CXDataPackageTypeConstant.定频宽带测向数据0x0515;
            ValueDescriptionManager.Registe<ushort, CXDataPackageTypeConstant>();
            string name;
            if (ValueDescriptionManager.TryGetNameByValue<CXDataPackageTypeConstant>(dataPackgeType, out name))
            {

            }
            else
            {
                name = $"未知的数据名类型\"0x{Convert.ToString((ushort)dataPackgeType, 16)}\"";
            }
        }

        /// <summary>
        /// 尝试获取扩展值对应的名称[获取成功返回true,获取失败返回false]
        /// </summary>
        /// <param name="value">值</param>
        /// <param name="group">描述组</param>
        /// <param name="name">值对应的名称</param>
        /// <returns>获取成功返回true,获取失败返回false</returns>
        protected override bool PrimitiveTryGetName(object value, IValueDescriptionGroup group, out string name)
        {
            try
            {
                int int32Value = (int)value;
                if (int32Value == 3)
                {
                    name = "Value3";
                    return true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            name = null;
            return false;
        }

        /// <summary>
        /// 尝试获取扩展值对应的描述[获取成功返回true,获取失败返回false]
        /// </summary>
        /// <param name="value">值</param>
        /// <param name="group">描述组</param>
        /// <param name="des">值对应的描述</param>
        /// <returns>获取成功返回true,获取失败返回false</returns>
        protected override bool PrimitiveTryGetDescription(object value, IValueDescriptionGroup group, out string des)
        {
            des = null;
            return false;
        }


        [DisplayNameEx("值1")]
        public const int VALUE1 = 1;

        [DisplayNameEx("值2", "值2的说明")]
        public const int VALUE2 = 2;


        /// <summary>
        /// 则向设备数据包类型
        /// </summary>
        [ValueDescriptionGroupAttribute("则向设备数据包类型")]
        public class CXDataPackageTypeConstant
        {
            private readonly static ValueDescriptionGroup<ushort> _group = null;
            static CXDataPackageTypeConstant()
            {
                _group = new ValueDescriptionGroup<ushort>(typeof(CXDataPackageTypeConstant), "则向设备数据包类型");
            }

            /// <summary>
            /// 获取数据包类型名称
            /// </summary>
            /// <param name="dataPackgeType">数据包类型</param>
            /// <returns></returns>
            public static string GetName(ushort dataPackgeType)
            {
                string name;
                try
                {
                    if (_group.TryGetNameByValue(dataPackgeType, out name))
                    {
                        return name;
                    }
                    else
                    {
                        return $"未知的数据名类型\"0x{Convert.ToString((ushort)dataPackgeType, 16)}\"";
                    }
                }
                catch (Exception ex)
                {
                    Loger.Warn(ex);
                    return $"未知的数据名类型\"0x{Convert.ToString((ushort)dataPackgeType, 16)}\"";
                }
            }


            /// <summary>
            /// 定频宽带测向数据0x0515
            /// </summary>
            [DisplayNameEx("定频宽带测向数据0x0515")]
            public const ushort 定频宽带测向数据0x0515 = 0x0515;

            /// <summary>
            /// 测试模式数据0x0516
            /// </summary>
            [DisplayNameEx("测试模式数据0x0516")]
            public const ushort 测试模式数据0x0516 = 0x0516;
        }
    }



}
